package com.distortiongames.level27.math
{
	/**
	 * Math.random() based random generator (slower than native functions)
	 * 
	 * This software is provided 'as-is', without any express or implied warranty.
	 * In no event will the authors be held liable for any damages arising from the use of this software.
	 *
	 * This file is released under the LGPL "GNU Lesser General Public License"
	 * More information can be found here: {@link http://www.gnu.org/copyleft/lesser.html}
	 *
	 * @author     Jordi Boggiano <j.boggiano@seld.be>
	 * @copyright  2007 Jordi Boggiano
	 * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
	 * @link       http://seld.be/
	 * @version    1.0.0
	 * @date       2007-12-16
	 * @package    com.seld.util
	 */
	public class Rand implements IRandGenerator
	{
		protected var
			rangeStart:uint = 0,
			rangeEnd:uint = 0;

		/**
		 * constructor
		 */
		public function Rand():void
		{
		}
		
		/**
		 * sets the range to be used
		 * 
		 * must be set before using 'ranged' to get random numbers
		 *
		 * @param min the range start
		 * @param max the range end
		 */
		public function setRange(min:uint, max:uint):void
		{
			if(min >= max)
				throw new Error('min can not be greater or equal to max');

			rangeStart = min;
			rangeEnd = max-min+1;
		}
		
		/**
		 * generates a random number between 0 and 0xffffffff
		 * 
		 * @return random int number
		 */
		public function get int32():uint
		{
			return uint(Math.random()*0xFFFFFFFF);
		}

		/**
		 * generates a random number between 0 and 1
		 *
		 * @return random float number
		 */
		public function get float():Number
		{
			return Math.random();
		}
		
		/**
		 * generates a random number comprised in the user defined range
		 * 
		 * @return random int number
		 */
		public function get ranged():uint
		{
			return rangeStart + Math.random() * rangeEnd;
		}
	}
}